<?php

namespace App\Http\Controllers\Admin;

require_once 'resources/org/code/Code.class.php';

use App\Http\Models\User;

class LoginController extends CommonController
{
    function login(){
        if($input = \Input::all()){
            $code = new \Code;
            $_code = $code->get();
            if($_code != strtoupper($input['code']))
                return back()->with ('msg', '验证码错误！');
            
            $user = User::where('user_name', $input['user_name'])->first();
            
            if(is_null($user))
                return back ()->with ('msg', '该用户不存在！');
            
            $password = md5($input['user_password']);
            if($user->user_password != $password)
                return back ()->with ('msg', '密码错误！');
            
            else
                session(['user' => $user]);
            return redirect('admin/index');
        }else{
            //$this->clear();
            return view('admin.login');
        }
    }
    
    function code(){
        $code = new \Code;
        $code->make();
    }
    
    public function clear(){
        session(['user'=> null]);
    }
    
    public function quit(){
        session(['user'=> null]);
        return redirect('admin/login');
    }
    
    
}
